
import axios from "axios";
import StorageUtils from "@/utils/storage_utils.js";
import { StorageTypeEnum } from "@/utils/storage_utils.js"
import RequestConfig from "@/request/request_config.js"
// 基于axios的网络请求基类
class BaseRequest {
    config = {
        baseURL: "http://rap2.taobao.org:38080/app/mock/119279/api/app/",// 接口地址
        headers: { "token": 456 }
    }
    newRequest = null; // 新的请求对象
    constructor(otherConfigs) {
        this._initRequest(otherConfigs);
    }
    // 初始化
    _initRequest(otherConfigs) {
        // 将传进来的config和全局的config合并
        const _config = Object.assign(this.config, otherConfigs);
        this.newRequest = axios.create(_config);
        this._setInterceptors();
    }
    // 设置拦截器
    _setInterceptors() {
        // 请求拦截
        this.newRequest.interceptors.request.use(config => {
            return config;
        }, error => {
            console.log(error);
            return Promise.reject(error)
        })
        // 响应拦截
        this.newRequest.interceptors.response.use(response => {
            let { status, data } = response;
            if (status == 200) {
                return data
            }
        }, error => {
            console.log(error);
            return Promise.reject(error)
        })
    }

    /**
     * 
     * @description ***展示型接口可用缓存,敏感型接口千万别缓存***
     * @param {Object} options = 
     * { 
     * 1.url:接口地址,
     * 2.openLocal:是否开启本地缓存[默认false],
     * 3.storageType:本地缓存类型[默认local],
     * 4.expireTime:本地存储超时时间[默认RequestConfig.defaultLocalExpireTime],
     * 5.isOnload:是否开启分页
     * 6.isFresh:是否是刷新
     * }
     * @returns
     * @memberof BaseRequest
     * @todo refresh和onload
     */
    async _request(options) {
        let { url, openLocal = RequestConfig.defaultOpenLocal, storageType = StorageTypeEnum.local, expireTime = RequestConfig.defaultLocalExpireTime } = options;
        let resData = null;
        if (openLocal) {
            let { localData, hasGotItem } = await StorageUtils.getStorageItem(url, storageType, { expireTime: expireTime });// {localdata符合条件是localdata,不符合条件是false}
            if (hasGotItem) {
                return localData
            } else {
                resData = await this.newRequest.request(options);
                await StorageUtils.setStorageItem(url, storageType, resData);//存
                return resData;
            }
        } else {
            resData = await this.newRequest.request(options);
            return resData;
        }
    }

    /**
     * @description get请求
     * @param {String} url 必传
     * @param {Object/String} params 可传 请求参数 [string]:a=123&b="b"
     * @param {Object} options 可传axios的参数加上openlocal
     * @returns
     * @memberof BaseRequest
     */
    async get(url, { params, options } = {}) {
        let _params = { ...params };
        let _options = {
            url: url,
            type: "get",
            params: _params,
            ...options
        }
        let resData = await this._request(_options);
        return resData;
    }
    /**
     * @description post请求
     * @param {String} url 必传
     * @param {Object} params 可传 请求参数
     * @param {Object} options 可传axios的参数加上openlocal
     * @returns 
     * @memberof BaseRequest
     */
    async post(url, { params, options } = {}) {
        let _params = { ...params }; // TODO::处理post参数
        let _options = {
            url: url,
            type: "post",
            data: _params,
            ...options
        }
        let resData = await this._request(_options);
        return resData;
    }

    /**
    * @description 检测网络状态
    * @memberof BaseRequest
    * @todo
    */
    checkNetWorkStatus() {

    }
    // 并发请求
    async requestAll(requestList = []) {
        let resData = await this._request(requestList);
        return resData;
    }

    /**
     *
     * @description 文件流下载
     * @param {String *} url 必传
     * @param {Object} params 可传 
     * @param {Object} options 可传 
     * @param {String} type 可传 
     * @memberof BaseRequest
     */
    async download(url, { params, options, type } = {}) {
        let _params = { ...params }; // TODO::处理post参数
        let _options = {
            url: url,
            type: "post",
            data: _params,
            responseType: "blob",
            ...options
        }
        let resData = await this._request(_options);
        let blob = new Blob([resData], { type: type });
        let downloadUrl = URL.createObjectURL(blob);
        return downloadUrl;
    }
    // upload 上传
    async upload() { }
}
export default BaseRequest;